package com.zz.framework.core.ext

import android.util.Log
import com.zz.framework.BuildConfig

/**
 * Copyright © 2019 zanezhao All rights reserved.
 * Created by zanezhao on 2019-11-30 17:57.
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */


fun Any.printJson(msg: String="") {
    if (BuildConfig.isShowLog.not()) {
        return
    }
    if (this==null||toString().isBlank()) {
        Log.e("zane", "blank json data")
        return
    }
    try {
        val message = this.toJson()
        Log.e("zane","${msg}:"+ message)
    } catch (e: Exception) {
        error("${e.cause?.message}${System.getProperty("line.separator")}$this.toString()")
    }
}

fun Any.print(msg: String="") {
    if (BuildConfig.isShowLog.not()) {
        return
    }
    try {
        val message = when {
            this is String -> {
               this
            }
            else -> this.toJson()
        }
        Log.e("zane", "${msg}:"+message)
    } catch (e: Exception) {
        error("${e.cause?.message}${System.getProperty("line.separator")}$this.toString()")
    }
}

fun Any.printError() {
    if (BuildConfig.isShowLog.not()) {
        return
    }
    if (this == null||toString().isBlank()) {
        Log.e("zane", ("<===== empty =====>"))
        return
    }
    when (this) {
        is Int, Long, Float, Double, Boolean -> Log.e("zane", ("${this.toString()}"))
        is String -> Log.e("zane", (toString()))
        else -> Log.e("zane", (toString()))
    }
}

fun Throwable.log(){
    if (BuildConfig.isShowLog.not()) {
        return
    }
    if (this == null) {
        Log.i("zane", "throwable is null")
        return
    }
    var className = this.stackTrace[1].fileName
    var methodName = this.stackTrace[1].methodName
    var lineNumber = this.stackTrace[1].lineNumber
    error("$methodName($className:$lineNumber) $this.message")
}


